In these exercises, we will be going through the dataset you preprocessed yesterday and perform three types of sentiment analysis:

  1. Basic sentiment analysis of text
  2. Slightly more elaborated sentiment analysis of text
  3. Experimental sentiment analysis on emoji

Exercise 1

Setup your R-session and load your data so that we can perform sentiment analysis. Assign the loaded data to a dataframe with the name comments.

You should use the options() function to prevent R from interpreting your character variables as factor variables. If you are not sure how to use the options() you can always search it in Rstudios help panel and have a look at all the different options there are. After setting the options, you should load your preprocessed dataset with readRDS(). Don’t forget to attach the necessary packages syuzhet and sentimentr.

Exercise 2

Chose the appropriate column from your comments dataframe to perform a basic sentiment analysis on. Which columns are suitable and which are not? Save comment sentiments in a new variable called BasicSentimentSyu and check whether the column has any zero values. If there are zero values, why might this be the case?

hyperlinks and emoji might cause Problems for sentiment analysis (or any textmining methods really). You can check whether a variable contains a given value x using the following approach table(variable == x) with your respective variable name.

Exercise 3

Check the documentation of the syuzhet package and the get_sentiment() function to see which dictionaries are available. Create a correlation matrix for sentiment scores using the different methods (you can leave out stanford). Which factors might result in low correlations between the dictionaries? Which one is the best to use?

You can find the documentation for the get_sentiment() function by searching for it’s name in the Rstudio help panel or by entering ?get_sentiment() in your console. You can also search online for further information. A correlation matrix can be created with the cor function. As this function needs a dataframe as an input, you need to create one variable for each sentiment dictionary rating and combine it into a dataframe with cbind.data.frame() before passing it to cor.

Exercise 4

Standardize the comment sentiments for the syuzhet method with respect to the total number of words in the respective comment. Call this new Variable SentimentPerWord.

Computing the number of words requires multiple functions if you want to use base R. The strplit() command splits a character string into multiple strings upon a specific indicator, for example a space (" "), the unlist() command transfers a list of values into a regular vector. The length() function counts the number of elements in a vector and with the sapply() function, you can apply a general function to each element of a vector. With these tools, you can compute the number of words per comment.

Exercise 5

Compute comment sentiments using the sentimentr package. Compare the average comment sentiment per word from the sentimentr package with the one we computed. Which one do you think is more trustworthy and why?

For a total sentiment score per comment, you first have to use the get_sentences() function and then use the sentiment_by() function on the sentences. To plot the two different scorings against each other, you need to put them into the same dataframe with cbind.data.frame() first. You can then use the ggplot() package for plotting.

Exercise 6

Load the emoji dictionary from the lexicon package and copy it to a new dataframe callend EmojiSentiments. Change the formatting of the dictionary entries and/or our Emoji column so that they are in the same format and can be matched. You can use the name EmojiToks for an intermediary variable if you need to create one. Afterwards, transform the EmojiSentiment dataframe to a quanteda dictionary object with the as.dictionary() function. Finally, use the tokens_lookup() function to create a new variable for emoji sentiments called EmojiToksSent

To see lexicons from the lexicon package, you can run lexicon::available_data() to get an overview of all the available lexicons. The name of the emoji lexicon is “emojis_sentiment”. Lexicons can be accessed with the command lexicon::lexicon_name usng the respective name of the lexicon you want to select. You can use the paste0() and gsub() functions to bring the formatting of the emoji column in line with the dictionary. Keep in mind that a valid dictionary needs appropriate column names, you can look this up in the help section of the as.dictionary() function.

Exercise 7

Plot the distribution of EmojiToksSent

You can use the simple hist() function to create a histogram. Keep in mind though that you need to transform the tokens object back to a regular numeric vector. You can do this with the unlist() and as.numeric() functions.